// 从localStorage获取购物车数据
export const getCartFromStorage = () => {
  try {
    const cartData = localStorage.getItem('cart');
    if (!cartData) return [];
    
    const parsedData = JSON.parse(cartData);
    
    // 检查是否过期（2小时有效期）
    const now = Date.now();
    const validItems = parsedData.filter(item => 
      now - item.timestamp < 2 * 60 * 60 * 1000
    );
    
    return validItems;
  } catch (error) {
    console.error('解析购物车数据失败:', error);
    return [];
  }
};

// 保存购物车数据到localStorage
export const saveCartToStorage = (cartItems) => {
  try {
    const dataWithTimestamp = cartItems.map(item => ({
      ...item,
      timestamp: Date.now()
    }));
    localStorage.setItem('cart', JSON.stringify(dataWithTimestamp));
  } catch (error) {
    console.error('保存购物车数据失败:', error);
  }
};